<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>diff() | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>diff()</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>diff()</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/displaying/difference/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/04-displaying/07-difference.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment().diff(Moment|String|Number|Date|Array);
moment().diff(Moment|String|Number|Date|Array, String);
moment().diff(Moment|String|Number|Date|Array, String, Boolean);
</code></pre>

							</div>
<p>&#x8981;&#x83B7;&#x53D6;&#x4EE5;&#x6BEB;&#x79D2;&#x4E3A;&#x5355;&#x4F4D;&#x7684;&#x5DEE;&#x5F02;&#xFF0C;&#x5219;&#x50CF;&#x4F7F;&#x7528; <code>moment#from</code> &#x4E00;&#x6837;&#x4F7F;&#x7528; <code>moment#diff</code>&#x3002;</p>
<pre><code class="language-javascript">var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
</code></pre>
<p>&#x8981;&#x83B7;&#x53D6;&#x53E6;&#x4E00;&#x4E2A;&#x5EA6;&#x91CF;&#x5355;&#x4F4D;&#x4E2D;&#x7684;&#x5DEE;&#x5F02;&#xFF0C;&#x5219;&#x5C06;&#x8BE5;&#x5EA6;&#x91CF;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x3002;</p>
<pre><code class="language-javascript">var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, &apos;days&apos;) // 1
</code></pre>
<p>&#x8981;&#x83B7;&#x53D6;&#x4E24;&#x4E2A;&#x65F6;&#x523B;&#x4E4B;&#x95F4;&#x7684;&#x5DEE;&#x503C;&#x7684;&#x65F6;&#x957F;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x5C06; <code>diff</code> &#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x7ED9; <code>moment#duration</code>&#x3002;
&#x6709;&#x5173;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x53C2;&#x9605; <a href="javascript:window.open('http://nodejs.cn/s/dud8hF');" target="_blank" rel="nofollow">moment#duration</a> &#x4E0A;&#x7684;&#x6587;&#x6863;&#x3002;</p>
<p>&#x652F;&#x6301;&#x7684;&#x5EA6;&#x91CF;&#x6709; <code>years</code>&#x3001;<code>months</code>&#x3001;<code>weeks</code>&#x3001;<code>days</code>&#x3001;<code>hours</code>&#x3001;<code>minutes</code> &#x548C; <code>seconds</code>&#x3002;
&#x4E3A;&#x4E86;&#x4FBF;&#x4E8E;&#x5F00;&#x53D1;&#xFF0C;&#x4ECE; <strong>2.0.0</strong> &#x7248;&#x672C;&#x5F00;&#x59CB;&#x652F;&#x6301;&#x5355;&#x6570;&#x5F62;&#x5F0F;&#x3002;
<strong>1.1.1</strong> &#x7248;&#x672C;&#x4E2D;&#x63D0;&#x4F9B;&#x4E86;&#x6BEB;&#x79D2;&#x4EE5;&#x5916;&#x7684;&#x5EA6;&#x91CF;&#x5355;&#x4F4D;&#x3002;</p>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;<code>moment#diff</code> &#x4F1A;&#x5C06;&#x7ED3;&#x679C;&#x622A;&#x65AD;&#x4E3A;&#x96F6;&#x4E2A;&#x5C0F;&#x6570;&#x4F4D;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x3002;
&#x5982;&#x679C;&#x9700;&#x8981;&#x6D6E;&#x70B9;&#x6570;&#xFF0C;&#x5219;&#x5C06; <code>true</code> &#x4F5C;&#x4E3A;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x3002;
&#x5728; <strong>2.0.0</strong> &#x4E4B;&#x524D;&#xFF0C;<code>moment#diff</code> &#x8FD4;&#x56DE;&#x7684;&#x6570;&#x5B57;&#x4F1A;&#x56DB;&#x820D;&#x4E94;&#x5165;&#x5230;&#x6700;&#x63A5;&#x8FD1;&#x7684;&#x6574;&#x6570;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x622A;&#x65AD;&#x7684;&#x6570;&#x5B57;&#x3002;</p>
<pre><code class="language-javascript">var a = moment([2008, 9]);
var b = moment([2007, 0]);
a.diff(b, &apos;years&apos;);       // 1
a.diff(b, &apos;years&apos;, true); // 1.75
</code></pre>
<p>&#x5982;&#x679C;&#x8BE5;&#x65F6;&#x523B;&#x65E9;&#x4E8E;&#x4F20;&#x7ED9; <code>moment.fn.diff</code> &#x7684;&#x65F6;&#x523B;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;&#x8D1F;&#x6570;&#x3002;</p>
<pre><code class="language-javascript">var a = moment();
var b = moment().add(1, &apos;seconds&apos;);
a.diff(b) // -1000
b.diff(a) // 1000
</code></pre>
<p>&#x8003;&#x8651;&#x8FD9;&#x4E00;&#x70B9;&#x7684;&#x4E00;&#x79CD;&#x7B80;&#x5355;&#x65B9;&#x6CD5;&#x662F;&#x5C06; <code>.diff(</code> &#x66FF;&#x6362;&#x4E3A;&#x51CF;&#x53F7;&#x8FD0;&#x7B97;&#x7B26;&#x3002;</p>
<pre><code class="language-javascript">          // a &lt; b
a.diff(b) // a - b &lt; 0
b.diff(a) // b - a &gt; 0
</code></pre>
<h4>&#x6708;&#x4EFD;&#x548C;&#x5E74;&#x4EFD;&#x7684;&#x5DEE;&#x5F02;</h4>
<p><code>moment#diff</code> &#x5BF9;&#x6708;&#x4EFD;&#x548C;&#x5E74;&#x4EFD;&#x7684;&#x5DEE;&#x5F02;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x7279;&#x6B8A;&#x5904;&#x7406;&#x3002;
&#x5B83;&#x505A;&#x4E86;&#x4E00;&#x4E9B;&#x4F18;&#x5316;&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x5177;&#x6709;&#x76F8;&#x540C;&#x65E5;&#x671F;&#x7684;&#x4E24;&#x4E2A;&#x6708;&#x59CB;&#x7EC8;&#x662F;&#x6574;&#x6570;&#x3002;</p>
<p>&#x56E0;&#x6B64;&#xFF0C;1&#x6708;15&#x65E5;&#x81F3;2&#x6708;15&#x65E5;&#x5E94;&#x8BE5;&#x6070;&#x597D;&#x662F;1&#x4E2A;&#x6708;&#x3002;</p>
<p>2&#x6708;28&#x65E5;&#x81F3;3&#x6708;28&#x65E5;&#x5E94;&#x8BE5;&#x6070;&#x597D;&#x662F;1&#x4E2A;&#x6708;&#x3002;</p>
<p>2011&#x5E74;2&#x6708;28&#x65E5;&#x81F3;2012&#x5E74;2&#x6708;28&#x65E5;&#x5E94;&#x8BE5;&#x6070;&#x597D;&#x662F;1&#x5E74;&#x3002;</p>
<p><a href="javascript:window.open('http://nodejs.cn/s/SFpca2');" target="_blank" rel="nofollow">&#x5728;&#x6B64;&#x5904;&#x67E5;&#x770B;&#x6709;&#x5173;&#x6708;&#x4EFD;&#x548C;&#x5E74;&#x4EFD;&#x7684;&#x5DEE;&#x5F02;&#x7684;&#x66F4;&#x591A;&#x8BA8;&#x8BBA;</a>&#x3002;</p>
<p>&#x5BF9;&#x6708;&#x4EFD;&#x548C;&#x5E74;&#x4EFD;&#x7684;&#x5DEE;&#x5F02;&#x7684;&#x66F4;&#x6539;&#x4E8E; <strong>2.0.0</strong>&#x3002;
&#x4ECE; <strong>2.9.0</strong> &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;diff &#x8FD8;&#x652F;&#x6301;&#x5B63;&#x5EA6;&#x5355;&#x4F4D;&#x3002;</p>
					</div>
          <div id="content_right">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment().diff(Moment|String|Number|Date|Array);
moment().diff(Moment|String|Number|Date|Array, String);
moment().diff(Moment|String|Number|Date|Array, String, Boolean);
</code></pre>

							</div>
<p>To get the difference in milliseconds, use <code>moment#diff</code> like you would use <code>moment#from</code>.</p>
<pre><code class="language-javascript">var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
</code></pre>
<p>To get the difference in another unit of measurement, pass that measurement as the second argument.</p>
<pre><code class="language-javascript">var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, &apos;days&apos;) // 1
</code></pre>
<p>To get the duration of a difference between two moments, you can pass <code>diff</code> as an argument into <code>moment#duration</code>. See the docs on <a href="#/durations/diffing/" target="_blank" rel="nofollow">moment#duration</a> for more info.</p>
<p>The supported measurements are <code>years</code>, <code>months</code>, <code>weeks</code>, <code>days</code>, <code>hours</code>, <code>minutes</code>, and <code>seconds</code>. For ease of development, the singular forms are supported as of <strong>2.0.0</strong>. Units of measurement other than milliseconds are available in version <strong>1.1.1</strong>.</p>
<p>By default, <code>moment#diff</code> will truncate the result to zero decimal places, returning an integer. If you want a floating point number, pass <code>true</code> as the third argument. Before <strong>2.0.0</strong>, <code>moment#diff</code> returned a number rounded to the nearest integer, not a truncated number.</p>
<pre><code class="language-javascript">var a = moment([2008, 9]);
var b = moment([2007, 0]);
a.diff(b, &apos;years&apos;);       // 1
a.diff(b, &apos;years&apos;, true); // 1.75
</code></pre>
<p>If the moment is earlier than the moment you are passing to <code>moment.fn.diff</code>, the return value will be negative.</p>
<pre><code class="language-javascript">var a = moment();
var b = moment().add(1, &apos;seconds&apos;);
a.diff(b) // -1000
b.diff(a) // 1000
</code></pre>
<p>An easy way to think of this is by replacing <code>.diff(</code> with a minus operator.</p>
<pre><code class="language-javascript">          // a &lt; b
a.diff(b) // a - b &lt; 0
b.diff(a) // b - a &gt; 0
</code></pre>
<h4>Month and year diffs</h4>
<p><code>moment#diff</code> has some special handling for month and year diffs. It is optimized to ensure that two months with the same date are always a whole number apart.</p>
<p>So Jan 15 to Feb 15 should be exactly 1 month.</p>
<p>Feb 28 to Mar 28 should be exactly 1 month.</p>
<p>Feb 28 2011 to Feb 28 2012 should be exactly 1 year.</p>
<p><a href="javascript:window.open('https://github.com/moment/moment/pull/571');" target="_blank" rel="nofollow">See more discussion on the month and year diffs here</a></p>
<p>This change to month and year diffs was made in <strong>2.0.0</strong>.
As of version <strong>2.9.0</strong> diff also support quarter unit.</p>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>